package com.peas.platform.module.icims.budget.archives.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.archives.adapter.repository.BudgetItemTypeRepository;
import com.peas.platform.module.icims.budget.archives.adapter.repository.po.BudgetItemTypePo;
import com.peas.platform.module.icims.budget.archives.domain.BudgetItemTypeDomain;
import com.peas.platform.module.icims.budget.archives.dto.BudgetItemTypeDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class BudgetItemTypeDomainImpl extends BaseDomainImpl<BudgetItemTypeDto, BudgetItemTypePo> implements BudgetItemTypeDomain {

    @Resource
    private BudgetItemTypeRepository budgetItemTypeRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<BudgetItemTypePo> getMapper() {
        return budgetItemTypeRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetItemTypePo>> getRepositoryClass() {
        return BudgetItemTypeRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetItemTypeDto dto) {
        if (StrUtil.isBlank(dto.getItemTypeCode())) {
            String code = sequenceService.genSequence("budget", "BudgetItemTypeDto", "BIT", 4, 1);
            dto.setItemTypeCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("itemTypeCode", dto.getItemTypeCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetItemTypeDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("itemTypeCode", dto.getItemTypeCode());
        return result;
    }

}
